home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3407 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 73700.776@compuserve.com (Walter C. Riley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: usage of ostrstream
  5. Date: Tue, 23 Jan 1996 21:37:30 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4e3kgr$90r@dub-news-svc-6.compuserve.com>
  8. References: <4e17t2$24v@crchh327.rich.bnr.ca>
  9. NNTP-Posting-Host: ad45-166.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Mike Hall <arnab@bnr.ca> wrote:
  13.  
  14. >Hi,
  15. >This is beginers query but any help is highly appreciated.
  16. >I am working on C++ in HP-UX environment.
  17. >I am trying to something like this in my code
  18. >f()
  19. >{
  20. > ostrstream oss;
  21.  
  22. > oss<<"The first string"<<ends
  23. > cout << oss.str()<<endl;
  24.  
  25. > oss.seekp(0);
  26. > oss<<"The second string in the same oss"<<ends;
  27. > cout << oss.str()<<endl;
  28. >}
  29.  
  30. >My test tool shows that the above has memory leaks.
  31. >On reading the man pages I found that once .str() is
  32. >called the buffer freezes and further usage of the oss
  33. >object is unpredictable.Also the user should delete the
  34. >char * returned by the .str().
  35.  
  36. >What I'd like to know is, a way to use/access the  ostrstream
  37. >string  multiply in a function without having to create a 
  38. >ostrstream type object everytime.
  39.  
  40. >Please E-Mail to arnab@bnr.ca
  41.  
  42. >Thanks in advance for your help/suggestions
  43. >Arnab
  44.  
  45. Amab, 
  46.   Try something like this:
  47.  
  48. char buf[1000];
  49. ostrstream oss(buf, sizeof(buf));
  50. oss << "The first string" << ends;
  51. cout << buf << endl;
  52. oss.seekp(0);
  53. oss<<"The second string in the same oss"<<ends;
  54. cout << buf << endl;
  55.  
  56. if you know the approx size of buf that you will need and it is not so
  57. large as to overflow the stack.  If buf will be very large,  you can
  58. use new to create buf, but remember to delete it when you are done.
  59.  
  60. Good luck,
  61. Walt
  62.  
  63.